package com.chen.gcode.fx.controllers;

import com.chen.gcode.utils.GCodeGenerate;
import com.chen.gcode.utils.Setting;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.scene.input.DragEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.DirectoryChooser;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;

public class SettingContorller implements Initializable {

    @FXML
    private AnchorPane pane;
    @FXML
    private TextField speedTextFiled;

    @FXML
    private TextField powerTextFiled;

    @FXML
    private TextField pathTextFiled;

    @FXML
    private TextField pointSizeTextFiled;





    @Override
    public void initialize(URL location, ResourceBundle resources) {
        speedTextFiled.setText(Setting.getSingleton().getSpeed());
        powerTextFiled.setText(Setting.getSingleton().getPower());
        pathTextFiled.setText(Setting.getSingleton().getSavePath());
        pointSizeTextFiled.setText(String.valueOf(Setting.getInstance().getPointSize()));
    }


    @FXML
    void getFile(DragEvent event) {
        List<File> files = event.getDragboard().getFiles();
        //获得文件
        File file = files.get(0);
        //之后的相关操作，获得文件路径等..
        if (file != null){
            pathTextFiled.setText(file.getPath());
        }
    }

    @FXML
    public void onclick(ActionEvent e){


        DirectoryChooser directoryChooser=new DirectoryChooser();
        File file = directoryChooser.showDialog(pane.getScene().getWindow());
        String path = file.getPath();//选择的文件夹路径

        if (path != null){
            pathTextFiled.setText(path);
        }

    }

    @FXML
    public void save(ActionEvent e){

        Float dstWidth = Float.valueOf(pointSizeTextFiled.getText());
        Setting.getInstance().setSpeed(speedTextFiled.getText());
        Setting.getInstance().setPower(powerTextFiled.getText());
        Setting.getInstance().setSavePath(pathTextFiled.getText());
        Setting.getInstance().setPointSize(dstWidth);
        Setting.getInstance().save();

    }

    public void openDir(){
        File file = new File(Setting.getInstance().getSavePath().trim());
        try {
            if (file != null && file.exists())
                Desktop.getDesktop().open(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void makeTest(){
        GCodeGenerate gCodeGenerate = new GCodeGenerate();
        gCodeGenerate.testCode(Setting.getInstance().getSavePath()+File.separator+"test.nc");
        openDir();
    }




}
